package com.tyf.tool;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 日期处理单元
 */
public class DateUtil
{
    public static final int SECOND = 1000;
    public static final int MINUTE = SECOND * 60;
    public static final int HOUR = MINUTE * 60;
    public static final long DAY = HOUR * 24;
    public static final long WEEK = DAY * 7;
    public static final long YEAR = DAY * 356;

    final static public String FULL_ST_FORMAT = "yyyy-MM-dd HH:mm:ss";
    final static public String FULL_J_FORMAT = "yyyy/MM/dd HH:mm:ss";
    final static public String CURRENCY_ST_FORMAT = "yyyy-MM-dd HH:mm";
    final static public String CURRENCY_J_FORMAT = "yyyy/MM/dd HH:mm";
    final static public String DATA_FORMAT = "yyyyMMddHHmmss";
    final static public String ST_FORMAT = "yyyy-MM-dd HH:mm";
    final static public String ST_CN_FORMAT = "yyyy年MM月dd日 HH:mm";
    final static public String CN_FORMAT = "yy年MM月dd日HH:mm";
    final static public String DAY_FORMAT = "yyyy-MM-dd";
    final static public String SHORT_DATE_FORMAT = "yy-MM-dd";
    final static public String YEAR_FORMAT = "yyyy";
    private static Calendar cal = Calendar.getInstance();
    public static String getDate(long second){
        SimpleDateFormat sdf = new SimpleDateFormat(FULL_ST_FORMAT);
        return sdf.format(new Date(second));
    }

    public static String getNow(){
        SimpleDateFormat sdf = new SimpleDateFormat(FULL_ST_FORMAT);
        return sdf.format(new Date());
    }
    public static String getDayFormatNow(){
        SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
        return sdf.format(new Date());
    }
    public static String getShortDate(){
        SimpleDateFormat sdf=new SimpleDateFormat(DAY_FORMAT);
        return sdf.format(new Date());
    }
    /**
	 * 获取"yyyy-MM-dd"格式的系统当前时间
	 * 
	 * @return
	 */
	public static String getDayFromatByDate(Date dateTime) {
		SimpleDateFormat format1 = new SimpleDateFormat(DAY_FORMAT);
		String dt1 = format1.format(dateTime);
		return dt1;
	}
	public static String getDateStrByDate(Date dateTime) {
		SimpleDateFormat format1 = new SimpleDateFormat(FULL_ST_FORMAT);
		String dt1 = format1.format(dateTime);
		return dt1;
	}
	/**
	 * 获取指定格式时间
	 * 
	 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 */
	public static Date stringToDate(String time) {
		SimpleDateFormat formatter = new SimpleDateFormat(FULL_ST_FORMAT);
		Date currentTime = new Date();
		try {
			currentTime = formatter.parse(time);
		} catch (Exception e) {
			System.out.println("DateUtil日期转换时参数异常："+time);
			e.printStackTrace();
		}
		return currentTime;
	}
	/**
	 * 取得日期后几天的日期,offset为天数偏移量
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date getLaterDate(Date date, int offset) {
		cal.setTime(date);
		cal.add(Calendar.DATE, offset);
		return cal.getTime();
	}
	/**
	 * 取得几分种前后的时间,offset为分种偏移量
	 * 
	 * @param date
	 * @param offset
	 * @return
	 */
	public static Date getLaterTime(Date date, int offset) {
		cal.setTime(date);
		cal.add(Calendar.MINUTE, offset);
		return cal.getTime();
	}
    /**
     * 求两个时间中间间隔的天数
     * @param time1
     * @param time2
     * @return
     */
    public static long getQuot(String time1, String time2){
		long quot = 0;
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date1 = ft.parse( time1 );
			Date date2 = ft.parse( time2 );
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Math.abs(quot);
    }
    /**
     * 比较两个字符串时间的大小，newTime>oldTime 返回true,否则返回false
     * @param firstTime
     * @param SecondTime
     * @return 
     */
    public static boolean compareTwoDateTime(String newTime,String oldTime){
    	SimpleDateFormat formatter = new SimpleDateFormat(DateUtil.FULL_ST_FORMAT);
    	boolean result = false;
    	try {
			long newDate = formatter.parse(newTime).getTime();
			long oldDate = formatter.parse(oldTime).getTime();
			result = newDate>oldDate ? true : false;
		} catch (ParseException e) {
			e.printStackTrace();
		}finally{
			return result;
		}
    	
    }
}
